<?php

class FDetail extends FWidget
{        
    
    private $handler;
    private $gridColumns;
    private $formFields;
    private $masterTable;
    private $masterIndexName;
    private $detailTable;
    private $detailIndexName;
    private $dataId;
    
    public function __construct($name, $title, $masterTable, $detailTable)
    {
        parent::__construct($name, 'fdetail');
        $this->setIsInput(true);
        $this->setTitle($title);
        $this->setMasterTable($masterTable);
        $this->setDetailTable($detailTable);
        
        $session = new FSessionManager($name);
        $session->setData(NULL);
    }
    
    public function generate()
    {
        $controller = new FController();
        
        $foreignColumns = $controller->obterChavesEstrangeirasDaTabela($this->getName());
        foreach ( $foreignColumns as $foreignColumn )
        {
            if ( $foreignColumn['table_ref'] == $this->getDetailTable() )
            {
                $this->setDetailIndexName($foreignColumn['attribute_ref']);
                $this->setDataId($foreignColumn['attribute']);
            }
            
            if ( $foreignColumn['table_ref'] == $this->getMasterTable() )
            {
                $this->setMasterIndexName($foreignColumn['attribute_ref']);
            }
        }
        
        $columns = array();
        $storeFields = array();
        foreach ( $this->getGridColumns() as $key => $gridColumn )
        {
            $columns[] = $gridColumn->generate();
            $storeFields[] = $key;
        }        
        $columns = implode(',', $columns);        
        $storeFields = implode("','", $storeFields);

        $fields = array();
        foreach ( $this->getFormFields() as $formField )
        {
            $fields[] = $formField->generate();
        }
        $fields = implode(',', $fields);
        
        if ( !$this->getWidth() )
        {
            $this->setWidth(400);
        }
        
        $jsCode = " {
            xtype: '{$this->getXtype()}',
            name: '{$this->getName()}',
            title: '{$this->getTitle()}',
            width: {$this->getWidth()},
            handlerUrl: '{$this->getHandler()}',
            gridColumns: [{$columns}],
            storeFields: ['$storeFields'],
            masterIndexName: '{$this->getMasterIndexName()}',
            masterTable: '{$this->getMasterTable()}',
            detailTable: '{$this->getDetailTable()}',
            detailIndexName: '{$this->getDetailIndexName()}',
            dataId: '{$this->getDataId()}',
            formFields: [$fields],
            formAction: '{$this->getFormAction()}',
        ";                
        
        $jsCode .= " } ";
        
        return $jsCode;
    }
    
    public function getHandler()
    {
        if ( !$this->handler )
        {
            $this->handler = 'src/php/detail.php';
        }
        
        return $this->handler;
    }

    public function setHandler($handler) {
        $this->handler = $handler;
    }
        
    public function getGridColumns() {
        return $this->gridColumns;
    }

    public function setGridColumns($gridColumns)
    {
        if ( is_array($gridColumns) )
        {
            foreach( $gridColumns as $key => $gridColumn )
            {
                $key = str_replace('.', '$', $key);
                $this->addGridColumn($key, $gridColumn);
            }
        }
    }
    
    public function addGridColumn($name, $gridColumn)
    {
        if ( $gridColumn instanceof FGridColumn )
        {
            $this->gridColumns[$name] = $gridColumn;
        }
    }

    public function getFormFields() {
        return $this->formFields;
    }

    public function setFormFields($formFields)
    {
        if ( is_array($formFields) )
        {
            foreach( $formFields as $formField )
            {
                $this->addFormField($formField);
            }
        }
        else
        {
            $this->addFormField($formFields);
        }
    }
    
    public function addFormField($formField)
    {
        if ( $formField instanceof FWidget )
        {
            $this->formFields[$formField->getName()] = $formField;
        }
    }
    
    public function getMasterTable() {
        return $this->masterTable;
    }

    public function setMasterTable($masterTable) {
        $this->masterTable = $masterTable;
    }
        
    public function getMasterIndexName() {
        return $this->masterIndexName;
    }

    public function setMasterIndexName($masterIndexName) {
        $this->masterIndexName = $masterIndexName;
    }
    
    public function getDetailTable() {
        return $this->detailTable;
    }

    public function setDetailTable($detailTable) {
        $this->detailTable = $detailTable;
    }

    public function getDetailIndexName() {
        return $this->detailIndexName;
    }

    public function setDetailIndexName($detailIndexName) {
        $this->detailIndexName = $detailIndexName;
    }
    
    public function getDataId() {
        return $this->dataId;
    }

    public function setDataId($dataId) {
        $this->dataId = $dataId;
    }
}

?>
